<?php
namespace app\api\service;
use app\api\service\Token;
use think\Request;
use app\api\model\User;
use app\api\model\Contact;
use app\api\model\Role;
use think\Cache;

class UserService
{
    public function page($page = 1, $size = 10){
        $data 	= Request::instance()->get();
        $where 	= [];

        empty($data['role']) || $where['role'] = $data['role'];
        empty($data['name']) || $where['username'] = ['like','%'.$data['name']];

        $obj = User::with(['frole', 'fcontact'])->where($where)
        ->paginate($size, false, ['page' => $page]);

        if ($obj->isEmpty()){
            return [];
        }
        return $obj->toArray();
    }

    // 改变用户
    public function changeStatus() {
        $param = Request::instance()->param();

        if (!isset($param['id']) || !isset($param['status'])) {
            return show(0, '错误', [], 200);
        }
        $obj = User::get($param['id']);
        $obj->status = $param['status'];
        
        if (!$obj->save()) {
            return show(0, '修改失败', [], 200);
        }
        return show(1, '修改成功', [], 200);
    }

    public function login(){
        
        Request::instance()->isPost() || die(show(0, '错误', [], 200));

        //获取参数
        $param = Request::instance()->param();
        
        $user  = User::with(['frole', 'fcontact'])
                ->where(['username' => $param['userName'] ])
                ->find();
        //检测用户是否存在
    	if( is_null($user) ){
            return show(0, '用户名不存在', [], 200);
        }
        
        //检测密码是否正确
    	if( $user['password'] != md5($param['password']) ){
            return show(0, '用户名或者密码错误', [], 200);	
        }

    	if( $user['status'] != 0 ){
            return show(0, '当前账户已被禁止', [], 200);	
        }
        // 保存token
        $token = Token::grantToken(json_decode($user), $user['access']);
        return show(1, '登录成功', [
            'user' => $user,
            'token' => $token], 200);
    }

    public function save(){ 
        $param = Request::instance()->param();	//获取参数

        if (!is_numeric($param['id']))
        {
            if (!isset($param['contact'])) {
                return show(0, '错误', [], 200);
            }
            // 新增
            $staff = new User;
            $staff->username     = $param['username'];
            $staff->password     = md5($param['username']);
            $staff->contact  = $param['contact'];
            $staff->role = $param['role'];

            if( $staff->save()){
                return show(1, '新增成功', [], 200);
            }
        }else {
            // 更新
            $staff = User::get($param['id']);
            $staff->role = $param['role'];
            // 检测错误
            if( $staff->save()){
                return show(1, '修改成功', [], 200);
            }
        }
        
        return show(0, '修改失败', [], 200);
    }
    
    public function logout() {
        $token = Request::instance()->header('token');
        cache($token, NULL);
        return show(1, 'ok', [], 200);
    }

    public function getUserInfo() {
        //获取参数
        $param = Request::instance()->param();

        $token = Request::instance()->header('token');
        $obj = json_decode(cache($token));
        $result = $obj->user;
        return show(1, '获取成功', [
            'name' => $result->username,
            'user_id' => $result->id,
            'access' => [$result->access],
            'token' => $token,
            'avator' => $result->avator
        ], 200);
    }
}